package com.gitee.francis.lw.form.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.francis.lw.form.model.request.FormGroupRItemRequestDTO;
import com.gitee.francis.lw.form.model.request.FormItemRequestDTO;
import com.gitee.francis.lw.form.model.request.TemplateConfigRequestDTO;
import com.gitee.francis.lw.form.service.IFormGroupRItemService;
import com.gitee.francis.lw.form.service.IFormItemService;
import com.gitee.francis.lw.form.service.ITemplateConfigService;
import com.gitee.francis.lw.mapper.FormGroupRItemMapper;
import com.gitee.francis.lw.mapper.FormItemMapper;
import com.gitee.francis.lw.mapper.TemplateConfigMapper;
import com.gitee.francis.lw.mapper.model.db.FormGroupRItemDO;
import com.gitee.francis.lw.mapper.model.db.FormItemDO;
import com.gitee.francis.lw.mapper.model.db.TemplateConfigDO;
import com.hzlemone.service.base.AbstractService;
import com.hzlemone.utils.JsonUtil;

/**
 * 
 * @ClassName FormItemServiceImpl
 * @Description 表单项模型。与数据表t_hz_lemon_e_form_item对应
 * @author 刘吴楚浩
 * @date Mon Jul 02 15:29:15 CST 2018
 * 
 */
@Service
@Transactional
public class FormItemServiceImpl extends
		AbstractService<FormItemDO, FormItemDO> implements IFormItemService {
	@Resource
	private FormItemMapper formItemMapper;

	@Autowired
	IFormGroupRItemService formGroupRItemService;

	@Autowired
	FormGroupRItemMapper formGroupRItemMapper;

	@Autowired
	TemplateConfigMapper templateConfigMapper;

	@Autowired
	ITemplateConfigService templateConfigService;

	@Override
	public void saveFormItem(FormItemRequestDTO formItemRequestDTO) {
		TemplateConfigRequestDTO templateConfigRequestDTO = new TemplateConfigRequestDTO();

		switch (formItemRequestDTO.getFormItemType()) {
		// 模板添加表单项
		case "1":
			formItemRequestDTO.setHzLemonEFormItemAttr(JsonUtil
					.toJson(formItemRequestDTO.getItemAttr()));
			String formItemId = save(formItemRequestDTO);
			templateConfigRequestDTO.setFormGroupOrItemId(formItemId);
			templateConfigRequestDTO.setTemplateId(formItemRequestDTO
					.getTemplateId());
			templateConfigRequestDTO.setHzLemonEIsFormGroup(formItemRequestDTO
					.getFormItemType());
			templateConfigRequestDTO.setHzLemonEFormItemSort(formItemRequestDTO
					.getItemSort());
			templateConfigRequestDTO.setCreatedTime(new Date());
			templateConfigService.save(templateConfigRequestDTO);
			break;
		// 模板添加表单组
		case "2":
			templateConfigRequestDTO.setFormGroupOrItemId(formItemRequestDTO
					.getGroupId());
			templateConfigRequestDTO.setTemplateId(formItemRequestDTO
					.getTemplateId());
			templateConfigRequestDTO.setHzLemonEIsFormGroup(formItemRequestDTO
					.getFormItemType());
			templateConfigRequestDTO.setHzLemonEFormItemSort(formItemRequestDTO
					.getItemSort());
			templateConfigRequestDTO.setCreatedTime(new Date());
			templateConfigService.save(templateConfigRequestDTO);
			break;
		// 组添加表单项
		case "3":
			FormGroupRItemRequestDTO formGroupRItemRequestDTO = new FormGroupRItemRequestDTO();
			formItemRequestDTO.setHzLemonEFormItemAttr(JsonUtil
					.toJson(formItemRequestDTO.getItemAttr()));
			formItemId = save(formItemRequestDTO);
			formGroupRItemRequestDTO.setFormItemId(formItemId);
			formGroupRItemRequestDTO
					.setHzLemonEGroupItemSort(formItemRequestDTO.getItemSort());
			formGroupRItemRequestDTO.setFormGroupId(formItemRequestDTO
					.getGroupId());
			formGroupRItemRequestDTO.setCreatedTime(new Date());
			formGroupRItemService.save(formGroupRItemRequestDTO);
			break;
		default:
			break;
		}

	}

	@Override
	public void updateFormItem(String id, FormItemRequestDTO formItemRequestDTO) {

		TemplateConfigDO templateConfigDO = null;
		switch (formItemRequestDTO.getFormItemType()) {
		case "1":
			templateConfigDO = templateConfigService.getById(id);

			templateConfigDO.setHzLemonEFormItemSort(formItemRequestDTO
					.getItemSort());
			templateConfigMapper.updateByPrimaryKeySelective(templateConfigDO);

			formItemRequestDTO.setHzLemonEFormItemAttr(JsonUtil
					.toJson(formItemRequestDTO.getItemAttr()));

			updateById(templateConfigDO.getFormGroupOrItemId(),
					formItemRequestDTO);
			break;
		case "2":
			templateConfigDO = templateConfigService.getById(id);

			templateConfigDO.setHzLemonEFormItemSort(formItemRequestDTO
					.getItemSort());
			templateConfigDO.setFormGroupOrItemId(formItemRequestDTO
					.getGroupId());
			templateConfigMapper.updateByPrimaryKeySelective(templateConfigDO);
			break;
		case "3":
			FormGroupRItemDO formGroupRItemDO = formGroupRItemService
					.getById(id);

			formGroupRItemDO.setHzLemonEGroupItemSort(formItemRequestDTO
					.getItemSort());
			formGroupRItemMapper.updateByPrimaryKeySelective(formGroupRItemDO);

			formItemRequestDTO.setHzLemonEFormItemAttr(JsonUtil
					.toJson(formItemRequestDTO.getItemAttr()));

			updateById(formGroupRItemDO.getFormItemId(), formItemRequestDTO);
			break;
		default:
			break;
		}

	}
}